package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;

import Utils.SecurityUtils;
import entity.Jingdong_user;
import service.Jingdong_userService;
import service.impl.Jingdong_userServiceImpl;

/**
 * Servlet implementation class Jingdong_userSys
 */
@WebServlet("/Jingdong_userSys")
public class Jingdong_userSys extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Jingdong_userService jService = new Jingdong_userServiceImpl();
		PrintWriter out = response.getWriter();
		String user = request.getParameter("user");
		System.out.println("进入servlet");
		switch (user) {
			case "add":
					String loginName = request.getParameter("name");
					String userName = request.getParameter("ddd");
					String password = request.getParameter("pwd");
					String identityCode = request.getParameter("sss");
					String mobile = request.getParameter("phone");
					String pwd = SecurityUtils.md5Hex3(password);
					Jingdong_user jingdong_user = new Jingdong_user(loginName, userName, pwd, identityCode, mobile);
					int result = jService.userAdd(jingdong_user);
					out.print(result);
				break;
			case "separate":
					String loginNames  = request.getParameter("name");
					int re = jService.userOnly(loginNames);
					if(re>0){
						out.print("用户名重复,请重新输入");
					}else{
						out.print("");
					}
				break;
			case "login":
					System.out.println("--------------进去登录验证-------------");
					String name = request.getParameter("names");
					String pwds = request.getParameter("pwds");
					pwd = SecurityUtils.md5Hex3(pwds);
					int iRet = jService.loginOnly(name, pwd);
					out.print(iRet);
				break;
			case "weiyi":
				System.out.println("保存名字,跳转首页");
				String loginname = request.getParameter("name");
				System.out.println(loginname);
				request.getSession().setAttribute("loginname",loginname);
				//request.setAttribute("loginname", loginname);
				request.getRequestDispatcher("indexs.jsp").forward(request, response);
				break;
			default:
				break;
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		doGet(request, response);
	}

}
